package CommonsdbutilsTest.MybatisPoject;

import CommonsdbutilsTest.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class MyBatisDao {
    //链接
    private static SqlSession sqlSession;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
        sqlSession = factory.openSession();

    }
    //日志
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }
    //方法
    //增加添加


    public static void add() throws IOException {
        rizhi();
        HashMap map = new HashMap();
        Scanner scanner = new Scanner(System.in);
        System.out.println("将对象的name新添加为");
        String name2 = scanner.next();
        System.out.println("将对象的sex新添加为");
        String sex2 = scanner.next();
        System.out.println("将对象的age新添加为");
        int age2 = scanner.nextInt();
        System.out.println("将对象的department新添加为");
        String department2 = scanner.next();
        map.put("sname", name2);//哈希map添加元素语句
        map.put("ssex", sex2);
        map.put("sage", age2);
        map.put("sdepartment",department2);
        int update = sqlSession.update("StuMapper.insertdata",map);//传参
        sqlSession.commit();
        System.out.println("更新的数据 " + update);
    }

    //查找
    public static void select() throws IOException {
        rizhi();
        List<Student> selectList = sqlSession.selectList("StuMapper.findAll");
        for (Student student : selectList) {
            System.out.println("student = " + student);
        }
    }

    //更新
    public static void update() throws IOException {
        rizhi();
        Student student = new Student();


        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要更新对象的name");
        String name1 = scanner.next();

        System.out.println("将对象的sex更新为");
        String sex1 = scanner.next();
        System.out.println("将对象的age更新为");
        int age1 = scanner.nextInt();
        System.out.println("将对象的department更新为");
        String department1 = scanner.next();

        student.setSsex(sex1);
        student.setSage(age1);
        student.setSdepartment(department1);
        student.setSname(name1);

        int updated = sqlSession.update("StuMapper.updatedata",student);
        System.out.println("updated = " + updated);



    }

    //删除
    public static void delete() throws IOException {
        rizhi();
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        System.out.println("输入你要删除对象的name");
        String sname = scanner.next();
        student.setSname(sname);
        int delete = sqlSession.delete("StuMapper.deletestudent",student);
        System.out.println("delete = " + delete);
    }

}
